package com.hua.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.error.CustomException;
import com.hua.pojo.Question;
import com.hua.pojo.QuestionCategory;
import com.hua.result.R;
import com.hua.service.QuestionCategoryService;
import com.hua.service.QuestionService;
import com.hua.utils.HuaUtil;
import com.hua.vo.ArticleData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 问答
 * @author ONE-X
 * @date 2020/11/10
 */
@Controller
@RequestMapping("/question")
public class QuestionController {

	@Autowired
	QuestionCategoryService questionCategoryService;
	@Autowired
	QuestionService questionService;

	@GetMapping("/list")
	public String toQuestionList(){
		return "question/list";
	}


	@ResponseBody
	@PostMapping("/list/{index}")
	public String questionList( @RequestParam(required = false) String categoryName,@PathVariable String index){
		IPage<Question> iPage = new Page<>();
		iPage.setSize(10);
		iPage.setCurrent(Long.parseLong(index));
		if(categoryName != null && categoryName.equals("all")){
			categoryName = null;
		}
		iPage = questionService.findQuestionPage(iPage, categoryName,null);
		List<Question> questionList = iPage.getRecords();
		R r = R.ok();

		Map<String, Object> map = new HashMap<>();
		map.put("questionList",questionList);
		map.put("total",iPage.getTotal());
		map.put("index",index);
		map.put("pages",iPage.getPages());
		r.setData(map);
		return JSON.toJSONString(r);
	}

	/**
	 * 一些页面基本的信息
	 * @return
	 */
	@ResponseBody
	@PostMapping("/info")
	public String questionInfo(){
		// 总问题数
		int count = questionService.count();
		// 问题类型
		List<QuestionCategory> questionCategoryList = questionCategoryService.list();
		String[] category = new String[questionCategoryList.size()];
		AtomicInteger i = new AtomicInteger();
		questionCategoryList.forEach((v)->{
			category[i.getAndIncrement()] = v.getCategory();
		});
		R r = R.ok();
		Map<String, Object> map = new HashMap<>();
		map.put("count",count);
		map.put("category",category);
		r.setData(map);
		return JSON.toJSONString(r);
	}

	/**
	 * 跳转到写问题页面
	 */

	@GetMapping("/write")
	public String toQuestionWrite(Model model){
		return "question/write";
	}
	/**
	 * 写问题
	 */
	@ResponseBody
	@PostMapping("write")
	public String writeQuestion(@RequestBody ArticleData articleData){
		try {
			if (articleData == null) {
				throw new NullPointerException();
			}
			questionService.saveQuestion(articleData);
		}catch (Exception e){
			e.printStackTrace();
			return JSON.toJSONString(R.error());
		}
		return JSON.toJSONString(R.ok());
	}
	/**
	 * 获取问题分类
	 */
	@ResponseBody
	@PostMapping("/getCategory")
	public String getCategory(){
		List<QuestionCategory> list = questionCategoryService.list();
		String[] category = new String[list.size()];
		AtomicInteger index = new AtomicInteger();
		list.forEach((v)->{category[index.getAndIncrement()] = v.getCategory();});
		return JSON.toJSONString(category);
	}

	/**
	 *  查看文章
	 */
	@GetMapping("/read/{qid}")
	public String toReadQuestion(@PathVariable String qid,Model model){
		// 查询文章
		if(qid == null){
			throw new CustomException("未知状态出现了异常");
		}
		Question question = questionService.getOne(new QueryWrapper<Question>().eq("qid", qid));
		if(question == null){
			throw new CustomException("未知博客页面");
		}
		model.addAttribute("question",question);
		return "question/read";
	}

	/**
	 * 查询当前用户的信息
	 */
	@ResponseBody
	@PostMapping("/currentUserInfo")
	public String findCurrentUserInfo(){
		String uid = HuaUtil.returnAuthentication();

		List<Question> questionList = questionService.list(new QueryWrapper<Question>().eq("author_id",uid).orderByDesc("gmt_create"));
		R r = R.ok();
		Map<String, Object> map = new HashMap<>();
		map.put("questionList",questionList);
		// 当前页

		r.data(map);
		return JSON.toJSONString(r);
	}

	/**
	 * 问题以及解决
	 * @param qid
	 * @return
	 */
	@ResponseBody
	@PostMapping("/solve/{qid}")
	public String solveQuestion(@PathVariable String qid){
		questionService.saveStatus(qid);
		return null;
	}

}
